package com.example.demo.math;

import org.apache.poi.xwpf.usermodel.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author yuhf
 */
public class MathOperation {

    public static void main(String[] args) {
       create(0, 320, 96, 4);
    }

    private static void create(int index, int total, int step, int cols){
        // 创建新的Word文档
        XWPFDocument document = new XWPFDocument();

        // 添加标题
        XWPFParagraph title = document.createParagraph();
        title.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun titleRun = title.createRun();
        titleRun.setText("Random Math Problems");
        titleRun.setBold(true);
        titleRun.setFontSize(18);

        // 添加空行
        document.createParagraph();
        // 计算表格行数
        int numRows = (int) Math.ceil((double) total / cols);

        // 创建表格
        XWPFTable table = document.createTable(numRows, cols);
        table.setWidth("100%");
        int createNum = 0;
        int rowIndex = 0;
        while (index < total){

            step = Math.min(step, total - createNum);
            // 生成题目
            List<String> problems = generateRandomProblems(step);

            // 填充表格
            int colIndex = 0;
            for (String problem : problems) {
                XWPFTableCell cell = table.getRow(rowIndex).getCell(colIndex);
                cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);

                XWPFParagraph paragraph = cell.getParagraphs().get(0);
                XWPFRun run = paragraph.createRun();
                int n = (int)Math.ceil((double) problems.size() / cols ) * colIndex + rowIndex - index / cols + index + 1;
                run.setText(n + ". " + problem);
                run.setFontSize(12);

                // 移动到下一个单元格
                colIndex++;
                if (colIndex >= cols) {
                    colIndex = 0;
                    rowIndex++;
                }
            }

            index += step;
            createNum += step;
        }


        // 保存Word文档到文件
        try {
            FileOutputStream out = new FileOutputStream("C:\\Users\\EDY\\Desktop\\RandomMathProblems4.docx");
            document.write(out);
            out.close();
            System.out.println("RandomMathProblems.docx created successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 生成随机题目
    private static List<String> generateRandomProblems(int numProblems) {
        List<String> problems = new ArrayList<>();
//        int numProblems = (int) (Math.random() * 31) + 50; // 随机生成50到80个题目
//        int numProblems = 320-288;
        for (int i = 0; i < numProblems; i++) {
            // 随机选择生成乘法、除法、加法或减法题目
            int type = (int) (Math.random() * 4);
            switch (type) {
                case 0:
                    problems.add(generateMultiplicationProblem());
                    break;
                case 1:
                    problems.add(generateDivisionProblem());
                    break;
                case 2:
                    problems.add(generateAdditionProblem());
                    break;
                case 3:
                    problems.add(generateSubtractionProblem());
                    break;
                default:
                    break;
            }
        }

        // 打乱题目顺序
        Collections.shuffle(problems);

        return problems;
    }

    // 生成乘法题目
    private static String generateMultiplicationProblem() {
        int threeDigitNumber = (int) (Math.random() * 900) + 100;
        int twoDigitNumber = (int) (Math.random() * 90) + 10;
        return threeDigitNumber + " × " + twoDigitNumber + " = ";
    }

    // 生成除法题目
    private static String generateDivisionProblem() {
        int threeDigitNumber2 = (int) (Math.random() * 9000) + 1000;
        int oneDigitNumber = (int) (Math.random() * 90) + 10;
        if(oneDigitNumber == 1){
            return generateDivisionProblem();
        }
        return threeDigitNumber2 + " ÷ " + oneDigitNumber + " = ";
    }

    // 生成加法题目
    private static String generateAdditionProblem() {
        double decimal1 = Math.random() * 100;
        double decimal2 = Math.random() * 100;
        // 保留三位小数
        decimal1 = Math.round(decimal1 * 1000.0) / 1000.0;
        // 保留三位小数
        decimal2 = Math.round(decimal2 * 1000.0) / 1000.0;
        return decimal1 + " + " + decimal2 + " = ";
    }

    // 生成减法题目
    private static String generateSubtractionProblem() {
        double decimal1 = Math.random() * 100;
        double decimal2 = Math.random() * 100;
        // 保留三位小数
        decimal1 = Math.round(decimal1 * 1000.0) / 1000.0;
        // 保留三位小数
        decimal2 = Math.round(decimal2 * 1000.0) / 1000.0;
        if(decimal1 < decimal2){
            return decimal2 + " - " + decimal1 + " = ";
        }
        return decimal1 + " - " + decimal2 + " = ";
    }


}
